package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.HallTypeVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.HallTypeServiceImp;
import com.school.movie.service.imp.SysLogServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/hallType")
public class HallTypeController {

    @Autowired
    HallTypeServiceImp hallTypeService;
    @Autowired
    SysLogServiceImp logService;

    @RequestMapping("/list")
    public String list(PageBean pager, HttpSession session, ModelMap map) {
        int cnt = hallTypeService.count();
        pager.setTotalRows(cnt);
        List<HallTypeVo> hallTypeList = hallTypeService.list(pager);
        pager.setData(hallTypeList);
        map.addAttribute("pager", pager);
        return "hallType";
    }

    @RequestMapping("/del")
    public String del(String ids, PageBean pager, ModelMap map) {

        System.out.println("ids=" + ids);
        String arr[] = ids.split(",");
        hallTypeService.del(arr);
        logService.add(BaseConstants.LOG_DEL, "删除了编号为：" + ids + "的考区");
        int cnt = hallTypeService.count();
        pager.setTotalRows(cnt);
        List<HallTypeVo> hallTypeList = hallTypeService.list(pager);
        pager.setData(hallTypeList);
        map.addAttribute("pager", pager);
        return "hallType";
    }

    @RequestMapping("/update")
    public String update(int hallTypeId, ModelMap map) {
        HallTypeVo hallType = hallTypeService.getById(hallTypeId);
        map.addAttribute("hallType", hallType);
        return "hallTypeAdd";
    }

    @RequestMapping("/init")
    public String init(ModelMap map) {
        return "hallTypeAdd";
    }

    @RequestMapping("/save")
    public String save(HallTypeVo hallType, ModelMap map) {
        if (hallType.getHallTypeId() > 0) {
            hallTypeService.update(hallType);
            logService.add(BaseConstants.LOG_UPDATE, hallType.toString());
        } else {
            hallTypeService.add(hallType);
            logService.add(BaseConstants.LOG_ADD, hallType.toString());
        }
        int cnt = hallTypeService.count();
        PageBean pager = new PageBean();
        pager.setTotalRows(cnt);
        List<HallTypeVo> hallTypeList = hallTypeService.list(pager);
        pager.setData(hallTypeList);
        map.addAttribute("pager", pager);
        return "hallType";
    }
}
